package org.southyin.demo02;

import org.southyin.utils.DBUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo03 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtils.getInstance().getConnection();
            // 管理事务
            conn.setAutoCommit(false);
            String sql = "update emp set ename = ? where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"leo");
            ps.setLong(2,10);
            // 执行语句 返回值是影响的记录行数
            int num = ps.executeUpdate();
            System.out.println("更新了 " + num + " 条记录");
            // 提交
            conn.commit();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        } finally {
            // 6.关闭连接
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
    }
}
